package team.pwy.service;

import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import team.pwy.consts.RbmConsts;
import team.pwy.mapper.MovieMapper;
import team.pwy.pojo.entity.Collect;
import team.pwy.pojo.entity.Movie;
import team.pwy.pojo.entity.Score;

import java.util.Date;
import java.util.Map;

@Component
public class MovieServiceImpl implements MovieService{

    @Resource
    private MovieMapper movieMapper;
    @RabbitHandler
    @RabbitListener(queues = RbmConsts.SEC_QUEUE_COLLECT)
    @Async
    public void addCollect(Map<String,Integer> map) {
        if (map.get("status") == 1) {
            Integer userId = map.get("userId");
            Integer id = map.get("movieId");
            Movie movie = movieMapper.findById(id);
            Collect collect = new Collect(id, userId, movie.getId(), movie.getTitle(), movie.getPosterImg(), movie.getPrice(), movie.getStatus(), movie.getCreateTime());
            movieMapper.addCollect(collect);
        } else {
            Integer userId = map.get("userId");
            Integer id =map.get("movieId");
            movieMapper.delCollect(id, userId);
        }
    }
  @RabbitHandler
    @RabbitListener(queues = RbmConsts.SCORE_QUEUE)
    @Async
    public void addScore(Map<String,Object> map){
        if(map.get("status").equals(1)){
            Integer userId= (Integer) map.get("userId");
            Integer movieId = (Integer) map.get("movieId");
            Double grade= (Double) map.get("grade");
            Score score=new Score(null,userId,movieId,grade,new Date());
            movieMapper.addScore(score);
        }else {
            Integer userId= (Integer) map.get("userId");
            Integer movieId = (Integer) map.get("movieId");
            movieMapper.delScore(movieId, userId);
        }
  }

}
